<?php
/**
 * Short description for file
 *
 * Long description for file (if any)...
 *
 * LICENSE: LGPL
 *
 * @category   Sabai
 * @package    Sabai
 * @copyright  Copyright (c) 2008 myWeb Japan (http://www.myweb.ne.jp/)
 * @author     Kazumi Ono <onokazu@gmail.com>
 * @license    http://opensource.org/licenses/lgpl-license.php GNU LGPL
 * @link
 * @since      File available since Release 0.1.1
*/

/**
 * Sabai_Log
 */
require 'Sabai/Log.php';

/**
 * Short description for class
 *
 * Long description for class (if any)...
 *
 * @category   Sabai
 * @package    Sabai
 * @copyright  Copyright (c) 2008 myWeb Japan (http://www.myweb.ne.jp/)
 * @author     Kazumi Ono <onokazu@gmail.com>
 * @license    http://opensource.org/licenses/lgpl-license.php GNU LGPL
 * @link
 * @version    0.1.9a2
 * @since      Class available since Release 0.1.1
 */
class Sabai
{
    /**
     * Initializes session and other required libraries
     *
     * @param int $logLevel
     * @param string $charset
     * @param mixed $locales string or array of locales
     * @static
     */
    function start($logLevel = SABAI_LOG_ERROR_ALL, $charset = 'UTF-8', $locales = 'en_US')
    {
        static $started = false;
        if (!$started) {
            set_magic_quotes_runtime(0);
            $_SERVER['PATH_INFO'] = !empty($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : (!empty($_SERVER['ORIG_PATH_INFO']) ? $_SERVER['ORIG_PATH_INFO'] : '');
            $escape = array('<'  => '%3C', '>'  => '%3E',  "'"  => '%27', '"'  => '%22', "\r" => '', "\n" => '');
            $_SERVER['PHP_SELF'] = strtr($_SERVER['PHP_SELF'], $escape);
            $_SERVER['PATH_INFO'] = strtr($_SERVER['PATH_INFO'], $escape);
            if (!session_id() && PHP_SAPI != 'cli') {
                @ini_set('session.use_only_cookies', 1);
                @ini_set('session.use_trans_sid', 0);
                @session_start();
            }
            require 'Sabai/ErrorHandler.php';
            require 'Sabai/URL.php';
            require 'Sabai/I18N.php';
            Sabai_ErrorHandler::initDefault();
            Sabai_Log::level($logLevel);
            define('SABAI_CHARSET', $charset);
            // required for bind_textdomain_codeset() ?
            @putenv('OUTPUT_CHARSET=' . SABAI_CHARSET);
            Sabai_I18N::locale($locales);
            $started = true;
        }
    }
}

/**
 * Converts a string into its plural form
 *
 * @param string $str
 * @return string
 */
function pluralize($str)
{
    if (strtolower(substr($str, -1)) == 'y') {
        return substr($str, 0, -1) . 'ies';
    }
    return $str . 's';
}

/**
 * Alias for htmlspecialchars()
 *
 * @param string $str
 * @param int $quoteStyle
 * @return string
 */
function h($str, $quoteStyle = ENT_QUOTES)
{
    return htmlspecialchars($str, $quoteStyle, SABAI_CHARSET);
}

/**
 * Echos out the result of h()
 *
 * @param string $str
 * @param int $quoteStyle
 * @return string
 */
function _h($str, $quoteStyle = ENT_QUOTES)
{
    echo h($str, $quoteStyle);
}

/**
 * HTML friendly var_dump()
 *
 * @param mixed $var
 */
function var_dump_html($var)
{
    echo '<pre>';
    var_dump($var);
    echo '</pre>';
}

/**
 * Checks if running PHP5
 *
 * @return bool
 */
function is_php5()
{
    return version_compare('5.0.0', phpversion(), '<=');
}

if (!is_php5()) {
    // Add PHP4 compatible clone function
    eval('
        function clone(&$object)
        {
            return $object;
        }
    ');
}

// array_intersect_key() from PHP5.1.0rc1
if (!function_exists('array_intersect_key')) {
    /**
     * PHP4 compatible array_intersect_key() function
     *
     * @return array
     */
    function array_intersect_key()
    {
        $ret = $array_keys = array();
        $array_args = func_get_args();
        foreach ($array_args as $i => $array_arg) {
            $array_keys[$i] = array_keys($array_arg);
        }
        foreach (call_user_func_array('array_intersect', $array_keys) as $key) {
            $ret[$key] = $array_args[0][$key];
        }
        return $ret;
    }
}